
var userModel = require('../models/user.server.model');
var crypto = require('crypto');
var redis = require('../../config/redis');
var utilsModel =require('../models/menu.server.model');
var formidable = require('formidable');
var fs = require('fs');
var os = require('os');
var type = os.type() == 'Linux' ? '/' : '\\';

module.exports = {
    removeUser:function(req , res , next){
        var params = req.query || req.param;
        userModel.removeUser(params,function(err,result){
            if(err){
                res.send({code:1,message:err.message});
            }else{
                res.send({code:0});
            }
        });
    },
    saveUser:function(req , res , next){
        var obj={};
        var form = new formidable.IncomingForm({
            encoding:"utf-8",
            uploadDir:"public/upload/images",  //文件上传地址
            maxFieldsSize:2*1024*1024, //设定最大文件限制
            keepExtensions:true  //保留后缀
        });
        form.parse(req)
        .on('field', function(name, value) {  // 字段
            if(name!='headfile'){
                obj[name] = value;
            }
        })
        .on('file', function(name, file) {  //文件;
            //obj[name] = file;
            var pathArr = file.path.split(type);
            obj.AVATAR = [];
            for(var p=1;p<pathArr.length;p++){
                obj.AVATAR.push(pathArr[p]);
            }
            obj.AVATAR = '/'+obj.AVATAR.join('/');
        })
        .on('error', function(error) {  //结束
            console.log("upload Err : " + error);
            if(error){
                throw error;
            }
        })
        .on('end', function() {  //结束
            if(obj.ID && obj.ID.length > 0){ //编辑
                    userModel.editUser(obj,function(result){
                        res.send(result);
                    });
                }else{ //新增
                    userModel.addUser(obj,function(result){
                        res.send(result);
                    });
                }
        });

    },
    getPower:function(req , res , next){
        var params = req.query || req.param;
        var userObj = req.session.user;
        utilsModel.getPower({roleId:userObj.user.ROLE_ID,menuId:params.menuId},function(result){
            res.send(result);
        });
    },
    getUserAll:function(req , res , next){
        var params = req.query || req.param;
        var userObj = req.session.user;
        userModel.getUserAll(params,function(result){
            redis.get('userRole',function(err,roles){
                if(err) throw err;
                res.send({result:result,role:userObj.role,roleList:JSON.parse(roles)});
            });
        });
    },
    getByUser : function(req , res , next){
        var param = req.body;
        var md5 = crypto.createHash('md5');
        md5.update(param.password,'utf-8');
        var val = md5.digest('hex');
        param.password = val.toUpperCase();
        userModel.getByUser(param , function(err,result){
            if(err){
                res.send({code:1,message:err.message});
            }else{
                req.session.user = result;
                res.send({code:0});
            }
        });
    },
    userModel:userModel
};

